/*
 * @Author: xiangru.xiao xiangru.xiao@mthreads.com
 * @Date: 2023-05-10 16:38:07
 * @LastEditors: xiangru.xiao xiangru.xiao@mthreads.com
 * @LastEditTime: 2023-05-24 14:13:49
 * Copyright (c) 2023 by Moore Threads, All Rights Reserved. 
 */
#ifndef CU_CHECK_H
#define CU_CHECK_H

#ifdef _WIN32
    #define CUDA_CALL __cdecl
    #define CUDA_API __declspec(dllexport)
#else
    #define CUDA_CALL
    #define CUDA_API
#endif
#include "cuda_runtime.h"
#include "cufft.h"
#include "cublas_v2.h"

CUDA_API int CUDA_CALL check_cuda_error (cudaError _err, int line,
                                   const char *func_name);
CUDA_API int CUDA_CALL check_cuda_error (cufftResult _err, int line,
                                   const char *func_name);
CUDA_API int CUDA_CALL check_cuda_error (cublasStatus_t _err, int line,
                                   const char *func_name);
class CUDAException{};

#define _PERF_CHECK_CUDA_ERROR_INNER(cond, func, line)                         \
    do                                                                         \
    {                                                                          \
        if (check_cuda_error (cond, line, func))                               \
            throw CUDAException();                                             \
    }                                                                          \
    while (0)

#define CHECK_CUDA_ERROR(cond) _PERF_CHECK_CUDA_ERROR_INNER(cond, __PRETTY_FUNCTION__, __LINE__)

#endif
